package threadCase;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @Author: WanqingLiu
 * @Date: 2023/02/04/12:06
 * 练习 Thread 实现多线程下载图片
 */
public class WebDownLoad extends Thread{
    private String url;
    private String fileName;
    public WebDownLoad(String url, String fileName){
        this.url = url;
        this.fileName = fileName;
    }
    @Override
    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downLoader(url, fileName);
        System.out.println("线程" + Thread.currentThread().getName() + "下载的文件名为" + fileName);
    }

    public static void main(String[] args) {
        new WebDownLoad("https://img-blog.csdnimg.cn/f45e39eb58bf4ff4bebbcd960740308d.png", "1.png").start();
        new WebDownLoad("https://img-blog.csdnimg.cn/1aa82e331f224ebc8950dbfcb07960f2.png", "2.png").start();
        new WebDownLoad("https://img-blog.csdnimg.cn/ad9ff5a182e54dda8d7cf73706411d97.png", "3.png").start();
    }
}

// 下载器
class WebDownLoader {
    public void downLoader(String url , String fileName) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常");
        }
    }
}

